package web.util;

import com.alibaba.fastjson.JSON;

import java.util.*;

/**
 * Created by Administrator on 2016/6/13.
 */
public class NewsUtil {
    public static void changeListNews(List<Map<String,Object>> lists,String search_key,String all_search_key){
        for (Map<String,Object> record : lists) {


                String strs=search_key+" "+all_search_key;
                List<String> list = StringOperateUtil.getKeyword(strs.trim());

                int size =list.size();
                String[] str = new String[size];
                for(int i=0;i<list.size();i++){
                    if(list.get(i).length()>0)
                        str[i]=list.get(i);
                }
                String titleStr="";
                if(record.get("title").toString().length()>40){
                    titleStr=record.get("title").toString().substring(0,40)+"...";
                }else{
                    titleStr=record.get("title").toString();
                }
                if(str!=null && str.length>0) {
                    record.put("title_key", HighlightToText.highlight(StringOperateUtil.exChange(titleStr), str));
                }

            if (Integer.parseInt(record.get("etime")+"") == 0) {
                record.put("endTime", "");
            }
            if(record.get("qualification")==null) {
                record.put("qualification", "");
            }
            if(record.get("qualification").toString().length()>15){
                record.put("qualification",record.get("qualification").toString().substring(0,15)+"...");
            }
        }
    }

    public static List<Map<String,Object>> findContactByJson(String json){
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
        List<Map<String,Object>> listNews=(List<Map<String,Object>>) JSON.parse(json);
        Set<String> set=new HashSet<String>();
        for (Map<String, Object> mapNews : listNews) {
            String contact=mapNews.get("subscriber_contact").toString();
            String phone=mapNews.get("subscriber_phone").toString();
            if(!set.contains(contact+phone)) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("contact", contact);
                map.put("phone", phone);
                list.add(map);
                set.add(contact+phone);
            }
        }
        return list;

    }

    public static List<Map<String,Object>> orderListNews(List<Map<String,Object>> list,String key){
//        用于返回list
        List<Map<String,Object>> listNews=new ArrayList<Map<String, Object>>();
//        用于排序list
        List<Map<String,Object>> listTemp=new ArrayList<Map<String, Object>>();

        String [] keys=key.split(" ");

        int scoreAll = keys.length;
        int time=DateUtil.getTimeByDay(Integer.parseInt(list.get(0).get("stime").toString()));
        for (Map<String, Object> map : list) {
            int stime = DateUtil.getTimeByDay(Integer.parseInt(map.get("stime").toString()));
            if(time!=stime ){
//                数据排序
                Collections.sort(listTemp,new Comparator<Map<String, Object>>() {
                    @Override
                    public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                        int map1value = (Integer) o1.get("score");
                        int map2value = (Integer) o2.get("score");
                        return map2value - map1value;
                    }
                });

                listNews.addAll(listTemp);
                listTemp.clear();
                time=stime;
            }
            if(map.get("content")==null){
                map.put("content","");
            }
//            通过关键词给正文打分
            String content = map.get("content").toString();
//            得分
            int score = 0;
//            多关键词
            int scoreNum = 0;
//           遍历关键词越靠前，分数越高，包含关键词越多，分数越高
            for(int i = 0 ; i < keys.length ; i++){
                String keyNum=keys[i].replace("+","");
                if(content.contains(keyNum)){
                    scoreNum++;
                    score += (scoreAll-i);
                }
            }
            if(scoreNum > 1){
                score=score * scoreNum * 100;
            }
            map.put("score",score);
            listTemp.add(map);
        }


//      将最后的数据翻到listNews中
        Collections.sort(listTemp,new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                int map1value = (Integer) o1.get("score");
                int map2value = (Integer) o2.get("score");
                return map2value - map1value;
            }
        });

        listNews.addAll(listTemp);
        listTemp.clear();

        return listNews;
    }

}
